// packageH/installationSever/installationSeverDetail/installationSeverDetail.js
var app = getApp();
var location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    order_id: 0,
    location: {},
    detailInfo: {},

    pictureShow: false,
    pictureList: [],

    codeShow: false,
    codeText: "",
    takeCodeImageUrl: "",
    custom_name: '',//安装自定义名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.order_id) {
      this.data.order_id = options.order_id;
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split("&");
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          console.log(chil_arr);
          if (chil_arr[0] == "order_id") {
            this.setData({
              order_id: chil_arr[1],
            });
          }
        }
      }
    }
    this._getLocation((mapdata, e) => {
      this.setData({
        "location.latitude": e.lat,
        "location.longitude": e.lng,
        "location.address": mapdata.address,
      });
      this.getDetails();
    });
    // this.setData({
    // 	location:{longitude: '110.02', latitude: '110.2',address:'模拟地址'}
    // })
    // this.getDetails();
    // return false;
  },

  setPictureShow() {
    this.setData({
      pictureShow: !this.data.pictureShow,
    });
  },

  setCodeShow() {
    this.setData({
      codeShow: !this.data.codeShow,
    });
  },

  makePhoneCall(evt) {
    let mobile = this.data.detailInfo.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile,
    });
  },
  openLocation(evt) {
    let {
      latitude,
      longitude
    } = this.data.detailInfo;
    wx.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  getDetails() {
    let {
      order_id,
      location
    } = this.data;
    let urlStr = app.getNetAddresss("plugin.live-install.frontend.worker-order.orderDetail");
    app._postNetWork({
      url: urlStr,
      data: {
        order_id,
        longitude: location.longitude,
        latitude: location.latitude
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        if (res.data.another_name && res.data.another_name.worker_name) {
          wx.setNavigationBarTitle({
            title: res.data.another_name.worker_name
          });
        }
        this.setData({
          detailInfo: res.data,
          custom_name: res.data.another_name.custom_name||'安装',
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  bindPreviewImage(evt) {
    let {
      arr,
      src
    } = evt.currentTarget.dataset;
    wx.previewImage({
      urls: arr,
      current: src
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  async bindBtnClick(evt) {
    console.log(evt);
    let {
      item,
      order_id
    } = evt.currentTarget.dataset;
    this.data._order_id = order_id;
    if (item.code == "installFinish") {
      this.setPictureShow();
      return;
    } else if (item.code == "workerChoose") {
      let confirmFlag = await app.confirm(`确定抢单吗？`);
      if (confirmFlag == false) return;
    } else if (item.code == "installStart") {
      let confirmFlag = await app.confirm(`确定${item.name}?`);
      if (confirmFlag == false) return;
    }
    let urlStr = app.getNetAddresss(item.api);
    app._postNetWork({
      url: urlStr,
      data: {
        order_id,
        source: 2
      },
      success: (resdata) => {
        let res = resdata.data;
        app.tips(res.msg);
        if (res.result !== 1) return;
        if (item.code == "takeCode") {
          let codeText = "取货码";
          this.setData({
            takeCodeImageUrl: res.data,
            codeShow: true,
            codeText
          });
        } else {
          this.getDetails();
        }
      },
    });
  },

  getPayCode() {
    let {
      order_id
    } = this.data.detailInfo;
    let urlStr = app.getNetAddresss("plugin.live-install.frontend.worker-order.payCode");
    app._postNetWork({
      url: urlStr,
      data: {
        order_id,
        source: 2
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return app.tips(res.msg);
        let codeText = "收款码";
        this.setData({
          takeCodeImageUrl: res.data,
          codeShow: true,
          codeText
        });
      },
    });
  },

  sendImageBtn() {
    let {
      pictureList,
      _order_id
    } = this.data;
    if (pictureList.length == 0) return app.tips(`请上传${this.data.custom_name}图片`);
    let urlStr = app.getNetAddresss("plugin.live-install.frontend.worker-order.finishInstall");
    app._postNetWork({
      url: urlStr,
      data: {
        order_id: _order_id,
        image: pictureList
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result !== 1) return app.tips(res.msg);
        this.setPictureShow();
        this.getDetails();
      },
    });
  },

  //点击售后完成
  async bindAfterBtnClick(evt) {
    let {
      btn,
      refund_id
    } = evt.currentTarget.dataset;

    //有些按钮需要弹窗二次确认
    if (btn.code == "finishRefund") {
      let confirmFlag = await app.confirm(`是否确定${btn.name}`);
      if (confirmFlag == false) return;
    }

    let urlStr = app.getNetAddresss(btn.api);
    app._postNetWork({
      url: urlStr,
      data: {
        refund_id
      },
      success: (resdata) => {
        let res = resdata.data;
        app.tips(res.msg);
        if (res.result !== 1) return;
        this.getDetails();
      },
    });
  },

  onRead_2() {
    wx.chooseImage({
      count: 9,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success: (res) => {
        // var tempFilePaths = res.tempFilePaths
        console.log(res.tempFilePaths);
        this.unload({
          tempFilePaths: res.tempFilePaths,
        });
      },
    });
  },
  //多张上传方法
  unload(data) {
    if (data.tempFilePaths.length == 0) return;
    wx.showLoading({
      title: "上传中",
    });
    let urlStr = app.getNetAddresss("upload.uploadPic");

    wx.uploadFile({
      url: urlStr,
      filePath: data.tempFilePaths[0],
      name: "file",
      formData: null,
      success: (resdata) => {
        var res = JSON.parse(resdata.data);
        let pictureList = this.data.pictureList;
        pictureList.push(res.data.img_url);
        this.setData({
          pictureList
        });
      },

      complete: (e) => {
        wx.hideLoading();
        data.tempFilePaths.shift();
        this.unload(data);
      },
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let order_id = this.data.order_id;
    return {
      path: "packageH/installationSever/installationSeverDetail/installationSeverDetail?order_id=" + order_id,
    };
  },
});